package com.gxuwz.medical.domain.chronicinfo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.chronicdis.*;
import com.gxuwz.medical.domain.person.Person;
import com.gxuwz.medical.exception.DbException;
import com.gxuwz.medical.tools.DateUtil;

/**
 * 慢病证信息管理模块类
 * 
 * @author 演示
 * 
 */
public class ChronicInfo {
	/**
	 * 慢病档案编号=农合证号(14位)+建档时间（14位）
	 */
	private String id;
	/**
	 * 农合证号
	 */
	private Person person;
	/**
	 * 疾病编码
	 */
	private Chronicdis chronicdis;
	/**
	 * 慢病起始时间
	 */
	private String starttime;
	/**
	 * 慢病治愈时间
	 */
	private String endtime;

	/**
	 * 状态:1表示正常、0表示注销
	 */
	private String status;
	/**
	 * 建档时间
	 */
	private java.util.Date creattime;
	/**
	 * 建档人
	 */
	private String creator;

	public ChronicInfo() {

	}

	// 全参构造函数
	public ChronicInfo(String id, Person person, Chronicdis chronicdis,
			String starttime, String endtime, String status, Date creattime,
			String creator) {

		this.id = id;
		this.person = person;
		this.chronicdis = chronicdis;
		this.starttime = starttime;
		this.endtime = endtime;
		this.status = status;
		this.creattime = creattime;
		this.creator = creator;
	}

	/**
	 * 构造函数，没有构造函数的话，在jsp引用实体类实例化的时候会报错，不能解析为一种类型,jsp写法： String
	 * id=request.getParameter("id"); User model=new User(id)
	 */

	public ChronicInfo(String id) throws Exception {
		this.id = id;
		try {
			loadDB();
		} catch (Exception e) {
			e.printStackTrace();
			throw new DbException("无法找到ID=" + this.id + "对应人员信息");
		}
	}

	// 加载信息
	private void loadDB() throws Exception {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DbUtil.getConn();
			pstmt = conn
					.prepareStatement("select * from t_chronicinfo where  id=?");
			int index = 1;
			pstmt.setString(index, this.id);

			rs = pstmt.executeQuery();
			while (rs.next()) { // 循环输出
				this.id = rs.getString("id");
				this.person = new Person(rs.getString("perscode"));
				this.chronicdis = new Chronicdis(rs.getString("illcode"));
				this.starttime = rs.getString("starttime");
				this.endtime = rs.getString("endtime");
				this.status = rs.getString("status");
				this.creattime = rs.getDate("creattime");
				this.creator = rs.getString("creator");
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(rs, pstmt, conn);
		}

	}

	/**
	 * 自己写的删除和修改的信息
	 * 
	 * @param areaname
	 * @throws Exception
	 */

	public void editChInfo() throws Exception {
		Connection conn = null;
		try {

			conn = DbUtil.getConn();
			// 开启手动提交事务
			conn.setAutoCommit(false);
			// 保存到数据库
			editToDB(conn);
			// 提交事务
			conn.commit();
		} catch (Exception e) {
			if (conn != null) {
				conn.rollback();
			}
			throw e;
		} finally {
			DbUtil.close(conn);
		}
	}

	public void delChInfo(String id) throws Exception {
		Connection conn = null;
		try {
			this.id = id;
			conn = DbUtil.getConn();
			// 开启手动提交事务
			conn.setAutoCommit(false);
			// 保存到数据库
			delFromDB(conn);
			// 提交事务
			conn.commit();
		} catch (Exception e) {
			if (conn != null) {
				conn.rollback();
			}
			throw e;
		} finally {
			DbUtil.close(conn);
		}
	}

	// 修改到数据库

	private void editToDB(Connection conn) throws SQLException {

		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer(
					"update t_chronicinfo set perscode=?,"
							+ "illcode=?,starttime=?,endtime=?,status=?,creattime=?,creator=? where id=?");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			pstmt.setString(1, this.person.getPerscode());
			pstmt.setString(2, this.chronicdis.getIllcode());
			pstmt.setString(3, this.starttime);
			pstmt.setString(4, this.endtime);
			pstmt.setString(5, this.status);
			pstmt.setDate(6, new java.sql.Date(this.creattime.getTime()));
			pstmt.setString(7, this.creator);
			pstmt.setString(8, this.id);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(pstmt);
		}
	}

	// 从数据库删除
	private void delFromDB(Connection conn) throws SQLException {

		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer(
					"delete from t_chronicinfo where id=?");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			pstmt.setString(1, this.id);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(pstmt);
		}
	}

	// 结束线--------------------------------------------

	/**
	 * 添加慢病证信息
	 * 
	 * @throws Exception
	 */
	public void addChroInfo() throws Exception {
		Connection conn = null;
		try {

			Date creattime = new Date();
			String no = DateUtil
					.format(creattime, DateUtil.DFMT_YYYYMMDDHHMMSS);
			// 慢病档案编号
			this.id = this.getPerson().getPerscode() + no;
			// 1:获取连接实例
			conn = DbUtil.getConn();
			// 2:持久化数据
			saveToDB(conn);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			// 关闭连接
			DbUtil.close(conn);
		}

	}

	/**
	 * 持久化数据
	 * 
	 * @param conn
	 * @throws SQLException
	 */
	private void saveToDB(Connection conn) throws SQLException {

		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer(
					"INSERT INTO t_chronicinfo (id,perscode, illcode, starttime, endtime,status,creattime,creator)");
			sqlBuff.append("VALUES (?, ?, ?, ?,?,?,?,?)");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			int index = 1;
			pstmt.setString(index++, this.id);
			pstmt.setString(index++, this.getPerson().getPerscode());
			pstmt.setString(index++, this.chronicdis.getIllcode());
			pstmt.setString(index++, this.starttime);
			pstmt.setString(index++, this.endtime);
			pstmt.setString(index++, this.status);
			pstmt.setDate(index++, new java.sql.Date(this.creattime.getTime()));
			pstmt.setString(index++, this.creator);
			int count = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally {
			DbUtil.close(pstmt);
		}
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public Chronicdis getChronicdis() {
		return chronicdis;
	}

	public void setChronicdis(Chronicdis chronicdis) {
		this.chronicdis = chronicdis;
	}

	public String getStarttime() {
		return starttime;
	}

	public void setStarttime(String starttime) {
		this.starttime = starttime;
	}

	public String getEndtime() {
		return endtime;
	}

	public void setEndtime(String endtime) {
		this.endtime = endtime;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public java.util.Date getCreattime() {
		return creattime;
	}

	public void setCreattime(java.util.Date creattime) {
		this.creattime = creattime;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

}
